package com.garrett.demo.sys.dao;

import com.garrett.demo.sys.entity.Account;
import com.garrett.demo.sys.entity.SimsStudent;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;

public class UserDaoTextImp implements UserDao{

    /**
     * 根据用户编号获取用户信息
     *
     * @param id 用户编号
     * @return 用户信息
     */
    @Override
    public SimsStudent getUserById(String id) {
        String path = "./small-login-step-07/src/main/resources/sims_student.txt";
        File file = new File(path);
        InputStreamReader read = null;
        try {
            if(file.isFile() && file.exists()){ //判断文件是否存在
                read = new InputStreamReader(
                        new FileInputStream(file), StandardCharsets.UTF_8);//考虑到编码格式
                BufferedReader bufferedReader = new BufferedReader(read);
                String lineTxt;
                while((lineTxt = bufferedReader.readLine()) != null) {
                    SimsStudent stu = recordFormat(lineTxt);
                    if(stu.getStudentId().equals(id)) return stu;
                }
            }else{
                System.out.println("找不到指定的文件");
            }
        } catch (Exception e) {
            System.out.println("读取文件内容出错");
            e.printStackTrace();
        } finally {
            if (read != null) {
                try {
                    read.close();
                } catch (IOException e) {
                    System.out.println("资源关闭异常");
                }
            }
        }
        return null;
    }

    /**
     * 单行数据记录处理
     * @param row 行记录
     * @return 学生信息
     */
    SimsStudent recordFormat(String row){
        SimsStudent stu = null;

        int beginIndex = row.indexOf("{") + 1;
        int endIndex = row.indexOf("}");
        String content = row.substring(beginIndex,endIndex);
        String[] split = content.split(",");
        // 知识点
        String[] fields = Arrays.stream(split).map(String::trim).toArray(String[]::new);
        if(fields.length > 0){
            stu = new SimsStudent();
            stu.setCollegeId(fields[0]);
            stu.setClassId(fields[1]);
            stu.setStudentId(fields[2]);
            stu.setStudentName(fields[3]);
            stu.setEngName(fields[4]);
            stu.setIdCardNo(fields[5]);
            stu.setMobilePhone(fields[6]);
        }
        return stu;
    }

    /**
     * 根据登录账号获取用户信息
     *
     * @param account 账户名
     * @return 用户信息
     */
    @Override
    public Account getUserByAccount(String account) {
        return null;
    }

    /**
     * 注册账户信息
     *
     * @param account 账户
     */
    @Override
    public void addAccount(Account account) {

    }

    /**
     * 添加学生信息
     *
     * @param student 学生信息
     */
    @Override
    public void createUser(SimsStudent student) {
        String path = "./small-login-step-07/src/main/resources/sims_student.txt";
        File file = new File(path);
        OutputStreamWriter osw = null;
        if(!file.exists()){
            System.out.println("数据源不存在!!!");
            throw new RuntimeException("数据源不存在");
        }

        try {
            // 创建基于文件的输出流
            FileOutputStream fos = new FileOutputStream(file,true);
            // 指定编码格式，以免读取时中文字符异常
            osw = new OutputStreamWriter(fos, StandardCharsets.UTF_8);
            osw.write(student.toString()+"\n");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(null != osw) {
                try {
                    osw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
